package best.driver.android.Activity.Order.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import best.driver.android.Beans.OrderAddresses;
import best.driver.android.Beans.OrderStatus;
import best.driver.android.R;
import butterknife.ButterKnife;
import butterknife.InjectView;

import java.util.ArrayList;
import java.util.List;

public class AddressDetailAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    private Context mContext;

    private List<OrderAddresses> mAddressItems = new ArrayList<OrderAddresses>();

    public AddressDetailAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<OrderAddresses> list) {
        mAddressItems.clear();
        mAddressItems.addAll(list);
        if(list == null || list.size() == 0) {
            return;
        }
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mAddressItems.size();
    }

    @Override
    public OrderAddresses getItem(int position) {
        return mAddressItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = mInflater.inflate(R.layout.item_multi_address_detail, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }

        setConvertView(holder, position);
        return convertView;
    }

    private void setConvertView(ViewHolder holder, int position) {
        OrderAddresses item = getItem(position);
        if(!TextUtils.isEmpty(item.address)) {
            holder.mTvAddress.setText(item.address);
        }
        if(OrderStatus.SHIPPER_ADDRESS.equals(item.flag)) {
            holder.mIvLeft.setImageResource(R.drawable.icon_carry_n);
        } else {
            holder.mIvLeft.setImageResource(R.drawable.icon_remove_n);
        }
    }

    static class ViewHolder {

        @InjectView(R.id.iv_left)
        ImageView mIvLeft;

        @InjectView(R.id.tv_address)
        TextView mTvAddress;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
